package com.baozun.houji.me.activity.address

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.base.BaseActivity
import com.baozhun.mall.common.ext.showToast
import com.baozhun.mall.common.model.bean.AddressInfo
import com.baozhun.mall.common.util.Constants
import com.baozhun.mall.common.widget.customview.addresspicker.AddressPickerDialog
import com.baozhun.mall.common.widget.customview.addresspicker.OnRegionPicked
import com.baozun.houji.me.BR
import com.baozun.houji.me.R
import com.baozun.houji.me.databinding.ActivityModifyAddressBinding
import com.baozun.houji.me.viewmodel.ModifyAddressViewModel
import java.util.regex.Pattern

@Route(path = RoutePath.Me.MODIFY_ADDRESS)
class ModifyAddressActivity : BaseActivity<ModifyAddressViewModel, ActivityModifyAddressBinding>() {

    var address: AddressInfo? = null

    override fun initView(savedInstanceState: Bundle?) {
        intent.getSerializableExtra(Constants.Common.BUNDLE_NAME)?.let {
            address = it as AddressInfo
        }
        if (address == null) {
            mDataBinding.titleBar.title = resources.getString(com.baozhun.mall.res.R.string.title_add_address)
            address = AddressInfo(0, "", "", "", "", "", ""
                , "", "", "", "", "", 0)
        } else {
            mDataBinding.titleBar.title = resources.getString(com.baozhun.mall.res.R.string.title_modify_address)
            mDataBinding.setVariable(BR.model, address)
            mDataBinding.deleteAddressTv.visibility = View.VISIBLE
            mDataBinding.setDefaultLayout.isSelected = address?.isDefault() == true
        }
        mViewModel.getRegion()
        mViewModel.getHotRegion()
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.area_layout, R.id.cover -> {
                mViewModel.hotRegionList.value?.let {
                    mViewModel.regionList.value?.let { it1 ->
                        AddressPickerDialog(this, address,
                            it, it1, object : OnRegionPicked{
                                override fun onPick(addressInfo: AddressInfo) {
                                    mDataBinding.areaEt.setText(addressInfo.getDetailAddress())
                                    address?.province = addressInfo.province
                                    address?.province_id = addressInfo.province_id
                                    address?.city = addressInfo.city
                                    address?.city_id = addressInfo.city_id
                                    address?.county = addressInfo.county
                                    address?.county_id = addressInfo.county_id
                                    address?.street = addressInfo.street
                                    address?.street_id = addressInfo.street_id
                                }
                            }
                        ).show()
                    }
                }
            }
            R.id.set_default_layout -> {
                mDataBinding.setDefaultLayout.isSelected = !mDataBinding.setDefaultLayout.isSelected
            }
            R.id.save_address_tv -> {
                if (verifyAddress()) {
                    mViewModel.saveAddress(getAddressInfo())
                }
            }
            R.id.delete_address_tv -> {
                address?.let {
                    mViewModel.deleteAddress(it.id.toString())
                }
            }

        }
    }

    private fun getAddressInfo() : AddressInfo {
        address?.receive_name = mDataBinding.nameEt.text.toString()
        address?.phone = mDataBinding.phoneEt.text.toString()
        address?.address = mDataBinding.detailEt.text.toString()
        address?.is_default = if (mDataBinding.setDefaultLayout.isSelected) 1 else 0
        return address as AddressInfo
    }

    private fun verifyAddress(): Boolean {
        if (mDataBinding.nameEt.text.isNullOrEmpty()) {
            showToast("请填写收货人姓名")
            return false
        }
        if (mDataBinding.phoneEt.text.isNullOrEmpty()) {
            showToast("请填写中国大陆手机号码")
            return false
        }
        if (!isPhoneNum(mDataBinding.phoneEt.text.toString())) {
            showToast("手机号码格式有误")
            return false
        }
        if (mDataBinding.areaEt.text.isNullOrEmpty()) {
            showToast("请选择省/市/区/街道")
            return false
        }
        if (mDataBinding.detailEt.text.isNullOrEmpty()) {
            showToast("请填写详细地址")
            return false
        }
        if (mDataBinding.detailEt.text.length <= 2) {
            showToast("详细地址不能少于2个字")
            return false
        }
        return true
    }

    private fun isPhoneNum(phone: String): Boolean {
        val compile = Pattern.compile("^(13|14|15|16|17|18|19)\\d{9}$")
        val matcher = compile.matcher(phone)
        return matcher.matches()
    }

    override fun createObserver() {

        mViewModel.defaultResult.observe(this) {
            finish()
        }

    }
}